<template>
  <div class="paddTwenCss">
   <div v-show="detailShow==undefined">
      <div>
        <el-form>
          <el-row class="rowFormItem">
            <el-col :span="3">
              <el-input v-model="keyword" placeholder="请输入昵称搜索" />
            </el-col>
            <!-- 提交按钮 -->
            <el-col :span="5" class="clear">
              <el-form-item style="text-align: center;width: 150px;margin-left: 10px;">
                <el-button type="primary" size="large" @click="handleSubmit()">检索</el-button>
                <el-button type="warning" size="large" style="margin-left: 8px" @click="handleReset()">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="tableClass">
      <el-table :data="formdata" v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}" border
        style="width: 100%;">
        <el-table-column align="center" prop="id" label="序号" />
        <el-table-column align="center" prop="photo_url" label="用户头像">
          <template slot-scope="scope">
            <el-button type="text" size="medium">
              <img style="width: 50%;" :src="scope.row.user_info.photo_url" />
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="user_info.nickname" label="昵称" />
        <el-table-column align="center" prop="user_info.sex_name" label="性别" />
        <el-table-column align="center" prop="phone" label="手机号码">
          <template slot-scope="scope">
            <div v-if="scope.row.phone ==null">未绑定</div>
            <div v-else>{{scope.row.phone}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="user_info.city" label="地区">
          <template slot-scope="scope">
            <div>{{scope.row.user_info.province}}/{{scope.row.user_info.city}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="reg_time" label="注册时间" />
        <el-table-column align="center" prop="vip_info" label="会员">
          <template slot-scope="scope">
            <div v-if="scope.row.vip_info!=''">
              <div v-for="(viptype,index) in scope.row.vip_info" :key="index">
<!--                <div v-if="viptype.type=='num'">-->
<!--                  {{viptype.type_name}}({{viptype.use_num}}/{{viptype.num}})-->
<!--                </div>-->
                <div v-if="viptype.type=='time'">
                  {{viptype.type_name}}
                </div>
              </div>
            </div>
            <div v-else>无</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="vip_info" label="会员到期">
          <template slot-scope="scope">
           <div v-if="scope.row.vip_info!=''">
              <div v-for="(viptype,index) in scope.row.vip_info" :key="index">
<!--                <div v-if="viptype.type=='num'">-->
<!--                  无限期-->
<!--                </div>-->
                <div v-if="viptype.type=='time'">
                  {{viptype.end_time}}
                </div>
              </div>
            </div>
            <div v-else>无</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <router-link :to="{path:'consumeUserList/courseLIstAndIntegralIndex',query:{userId:scope.row.id}}">
              <el-button class="btnAppeal" size="small" type="text">详情</el-button>
            </router-link>
            <el-button class="btnAppeal" size="small" type="text" @click="setVip(scope.row.id)">设置会员</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加模态款 -->
      <el-dialog style="margin: auto;" :close-on-click-modal="false" :title="title" :visible.sync="modelMsg">
        <div class="msgDiv">
          <table>
            <tr>
              <th>会员类型</th>
              <th>价格</th>
              <th>次数/到期时间</th>
              <th>开关</th>
            </tr>
<!--            <tr>-->
<!--              <td>次数会员</td>-->
<!--              <td>-->
<!--                <el-input-number class="inputNumer" :disabled="!numCheck" v-model="vip_arrNum.price" size="small" :precision="2" :step="1"-->
<!--                  :controls="false"></el-input-number>元-->
<!--              </td>-->
<!--              <td>-->
<!--                最高使用-->
<!--                <el-input-number class="inputNumer" :disabled="!numCheck" v-model="vip_arrNum.num" size="small" :precision="0" :step="1"-->
<!--                  :controls="false"></el-input-number>次-->
<!--              </td>-->
<!--              <td>-->
<!--                <el-switch v-model="numCheck" active-color="#13ce66" inactive-color="#ff4949"></el-switch>-->
<!--              </td>-->
<!--            </tr>-->
            <tr>
              <td>年费会员</td>
              <td>
                <el-input-number class="inputNumer"  :disabled="!timeCheck" v-model="vip_arrTime.price" size="small" :precision="2" :step="0.1"
                  :controls="false"></el-input-number>元
              </td>
              <td>
                <el-date-picker style="margin: auto;"  :disabled="!timeCheck" v-model="vip_arrTime.end_time" type="datetime" placeholder="选择日期时间"
                  align="right" value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions">
                </el-date-picker>
              </td>
              <td>
                <el-switch v-model="timeCheck" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
              </td>
            </tr>
          </table>
          <el-button type="success" @click="addMsg">{{addORupdate}}</el-button>
        </div>
      </el-dialog>
      <br>
      <!-- 分页 -->
      <div style="text-align: center;">
        <el-pagination background layout="prev, pager, next" @current-change="pageChange" :page-size="pageSize" :total="totalusers" />
      </div>
    </div>
   </div>
   <div v-show="detailShow >0">
     <router-view></router-view>
   </div>
    <!-- </el-card> -->
  </div>
</template>

<script>
  export default {
    data() {
      return {
        loading: false,
        keyword: "",
        title: "设置会员",
        addORupdate: "提交",
        modelMsg: false,
        userID: "", //当前选中的会员
        vip_arr: [],
        vip_arrNum: {
          type: "num",
          price: "",
          num: "",
        },
        vip_arrTime: {
          type: "time",
          price: "",
          end_time: ""
        },
        numCheck: false, //选择次数会员
        timeCheck: false, //年费会员
        pickerOptions: {
          shortcuts: [{
              text: '七天会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
                picker.$emit('pick', date);
              }
            },
            {
              text: '一个月会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 30);
                picker.$emit('pick', date);
              }
            },
            {
              text: '三个月会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 90);
                picker.$emit('pick', date);
              }
            },
            {
              text: '六个月会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 180);
                picker.$emit('pick', date);
              }
            },
            {
              text: '一年会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 365);
                picker.$emit('pick', date);
              }
            }
          ]
        },
        formdata: [],
        detailShow: undefined,
        totalusers: 0, //总行数
        pageSize: 10, //每页几行
        startpage: 1, //起始页
      }
    },
    created() {
      this.detailShow = this.$route.query.userId
      if(this.detailShow==undefined){
        this.getConsumeUserList()
      }
    },
    methods: {
      addMsg() {
        /* 显示加载中。。。 */
        const loadingOne = this.$loading({
          lock: true,
          text: '设置中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
          this.vip_arr = []
          console.log(this.vip_arrNum)
          console.log(this.vip_arrTime)
          if (this.numCheck == true) {
            this.vip_arr.push(this.vip_arrNum)
          }
          if (this.timeCheck == true) {
            this.vip_arr.push(this.vip_arrTime)
          }
          let params = {
            user_id: this.userID,
            vip_arr: this.vip_arr
          }
          console.log(params)
          this.$api.setUserVip(params).then(res=>{
              this.$message({
                type: res.data.level,
                message: res.data.message
              });
              if (res.data.level == "success") {
                this.getConsumeUserList()
                loadingOne.close()
                this.modelMsg = false
              } else {
                this.modelMsg = true
                loadingOne.close()
              }
          })

      },
      //设置会员
      setVip(userId) {
        this.modelMsg = true
        this.userID = userId
       this.numCheck=false
       this.vip_arrNum.price=""
       this.vip_arrNum.num=""
       this.timeCheck=false
       this.vip_arrTime.price=""
       this.vip_arrTime.end_time=""

        this.$api.getUserVipList({user_id:userId}).then(res=>{
          res.data.data.forEach(item=>{
              if(item.type=="num"){
                  this.numCheck=true
                  this.vip_arrNum.price=item.price
                  this.vip_arrNum.num=item.num
              }
              if(item.type=="time"){
                  this.timeCheck=true
                  this.vip_arrTime.price=item.price
                  this.vip_arrTime.end_time=item.end_time
              }
          })
        })
      },
      //搜索
      handleSubmit() {
        this.startpage = 1
        this.getConsumeUserList()
      },
      handleReset() {
        this.keyword = ""
      },
      //获取分页列表
      getConsumeUserList() {
        let params = {
          "page": this.startpage,
          "limit": this.pageSize,
          "keyword": this.keyword
        }
        //调用后台提供的接口方法
        this.$api.getConsumeUserList(params).then(res => {
          if (res.data.level == "success") {
            this.formdata = res.data.data
            this.totalusers = res.data.page_info.total
            this.loading = false
          } else {
            this.$message.error(res.data.message)
          }


        })
      },
      //分页 pageChange当前页发生改变触发函数
      async pageChange(page) {
        this.startpage = page
        this.loading = true
        //调用获取列表的方法
        this.getConsumeUserList()
      },
    }
  }
</script>
<style scoped="scoped">

  /deep/.el-card__header {
    background-color: #409EFF;
    color: whitesmoke;
  }

  /deep/.el-dialog {
    margin-top: 15vh !important;
    width: 45%;
    min-width: 555px;
  }

  /deep/.el-dialog__body {
    padding: 20px 20px 50px;
  }

  .msgDiv {
    width: 80%;
    margin: auto;
    text-align: center;
  }

  .el-input {
    display: flex;
    flex-grow: <2>;
  }

  table {
    width: 100%;
    font-size: 16px;
    margin-bottom: 40px;
  }

  table th {
    min-width: 40px;
  }

  table tr {
    height: 50px;
  }

  table tr td {
    margin-right: 10px;
  }

  .inputNumer {
    margin: 0 6px;
  }
  .btnAppeal{
    margin-left: 10px;
  }
  .paddTwenCss{
    padding: 12px 5px 5px;
  }
</style>
